package com.quotes.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.EventListenerList;
import com.quotes.dates.TimePeriod;
import com.quotes.dates.TimeRange;
import com.quotes.dates.TimeRangeFactory;

public class TimeRangeSelectorPanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private TimeRange range = TimeRangeFactory.untilToday(TimePeriod.ONE_YEAR);
	private EventListenerList listenerList = new EventListenerList();
	
	public TimeRangeSelectorPanel() {
		this.createElements();
	}

	private void createElements() {
		ButtonGroup buttonGroup = new ButtonGroup();
		
		JRadioButton oneMonthRadioButton = new JRadioButton("1m");
		oneMonthRadioButton.addActionListener(this);
		buttonGroup.add(oneMonthRadioButton);
		add(oneMonthRadioButton);
		
		JRadioButton threeMonthRadioButton = new JRadioButton("3m");
		threeMonthRadioButton.addActionListener(this);
		buttonGroup.add(threeMonthRadioButton);
		add(threeMonthRadioButton);

		JRadioButton sixMonthRadioButton = new JRadioButton("6m");
		sixMonthRadioButton.addActionListener(this);
		buttonGroup.add(sixMonthRadioButton);
		add(sixMonthRadioButton);

		JRadioButton oneYearRadioButton = new JRadioButton("1y");
		oneYearRadioButton.setSelected(true);
		oneYearRadioButton.addActionListener(this);
		buttonGroup.add(oneYearRadioButton);
		add(oneYearRadioButton);

		JRadioButton twoYearRadioButton = new JRadioButton("2y");
		twoYearRadioButton.addActionListener(this);
		buttonGroup.add(twoYearRadioButton);
		add(twoYearRadioButton);
		
		JRadioButton threeYearRadioButton = new JRadioButton("3y");
		threeYearRadioButton.addActionListener(this);
		buttonGroup.add(threeYearRadioButton);
		add(threeYearRadioButton);
	}
	
	public TimeRange getTimeRange() {
		return this.range;
	}
	
	public void addTimeRangeSelectionChangedListener(TimeRangeSelectionChangedListener listener) {
        listenerList.add(TimeRangeSelectionChangedListener.class, listener);
    }
    
    public void removeTimeRangeSelectionChangedListener(TimeRangeSelectionChangedListener listener) {
        listenerList.remove(TimeRangeSelectionChangedListener.class, listener);
    }
    
    private void fireTimeRangeSelectionChangedEvent(TimeRange newRange) {
        Object[] listeners = listenerList.getListenerList();

        for (int i=0; i<listeners.length; i+=2) {
            if (listeners[i] == TimeRangeSelectionChangedListener.class) {
                ((TimeRangeSelectionChangedListener)listeners[i+1]).selectionChanged(newRange);
            }
        }
    }
    
	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getActionCommand() == "1m") {
			this.range = TimeRangeFactory.untilToday(TimePeriod.ONE_MONTH);
			fireTimeRangeSelectionChangedEvent(range);
		} else if (event.getActionCommand() == "3m") {
			this.range = TimeRangeFactory.untilToday(TimePeriod.THREE_MONTHS);
			fireTimeRangeSelectionChangedEvent(range);
		} else if (event.getActionCommand() == "6m") {
			this.range = TimeRangeFactory.untilToday(TimePeriod.SIX_MONTHS);
			fireTimeRangeSelectionChangedEvent(range);
		} else if (event.getActionCommand() == "1y") {
			this.range = TimeRangeFactory.untilToday(TimePeriod.ONE_YEAR);
			fireTimeRangeSelectionChangedEvent(range);
		} else if (event.getActionCommand() == "2y") {
			this.range = TimeRangeFactory.untilToday(TimePeriod.TWO_YEARS);
			fireTimeRangeSelectionChangedEvent(range);
		}else if (event.getActionCommand() == "3y") {
			this.range = TimeRangeFactory.untilToday(TimePeriod.THREE_YEARS);
			fireTimeRangeSelectionChangedEvent(range);
		}
	}
}
